package com.studentachievement.servlets;

import com.studentachievement.beans.Admin;
import com.studentachievement.beans.Student;
import com.studentachievement.dao.AdminDAO;
import com.studentachievement.dao.StudentDAO;
import com.studentachievement.utils.DatabaseUtil;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String notok = null;
        HttpSession sessionNotOk = request.getSession();

        // 获取表单提交的数据
        String role = request.getParameter("login");  // 获取角色
        String username = request.getParameter("username");  // 获取用户名
        String password = request.getParameter("password");  // 获取密码
//        // 输出表单提交的数据
//        out.println("<html><body>");
//        out.println("<h1>表单提交成功</h1>");
//        out.println("<p>角色: " + role + "</p>");
//        out.println("<p>用户名: " + username + "</p>");
//        out.println("<p>密码: " + password + "</p>");
        //
        RequestDispatcher dispatcherToOKjsp = null;
        //实现登录逻辑
        if(role.equals("admin")){//如果当前要登录的是管理员账号
            Admin admin = new Admin();
            admin = AdminDAO.getAdminByUsernameAndPassword(username, password);
            if(admin != null){
//                out.println(admin.string());
//                out.println("</body></html>");
                // 将 admin 对象存入 session
                HttpSession session = request.getSession();
                session.setAttribute("admin", admin);

                // 使用 RequestDispatcher 转发到 JSP 页面
                RequestDispatcher dispatcher = request.getRequestDispatcher("/AdminHomeServlet");
                try {
                    dispatcher.forward(request, response);
                } catch (ServletException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else{
//                out.println("<p>ERROR"+ "--没有此管理员" +"</p>");
//                out.println("</body></html>");
                notok = "查无此管理员";
                sessionNotOk.setAttribute("notok", notok);
                dispatcherToOKjsp = request.getRequestDispatcher("notok.jsp");
                try {
                    dispatcherToOKjsp.forward(request, response);
                } catch (ServletException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        else if(role.equals("user")){//如果当前登录的是普通用户
            Student student = new Student();
            student = StudentDAO.getStudentByUsernameAndPassword(username, password);
            if(student != null){
                // 将 admin 对象存入 session
                HttpSession session = request.getSession();
                session.setAttribute("student", student);

                RequestDispatcher dispatcher = request.getRequestDispatcher("/StudentHomeServlet");
                try {
                    dispatcher.forward(request, response);
                } catch (ServletException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            else{
//                out.println("<p>ERROR"+ "--没有此用户" +"</p>");
//                out.println("</body></html>");
                notok = "查无此用户";
                sessionNotOk.setAttribute("notok", notok);
                dispatcherToOKjsp = request.getRequestDispatcher("notok.jsp");
                try {
                    dispatcherToOKjsp.forward(request, response);
                } catch (ServletException | IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        doGet(request, response);
    }
}
